package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.CategoryServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryServiceImpl categoryService;

    /**
     * 新增分类 1、菜品 2、套餐
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增的分类数据为:{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result findPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询的数据为:{}", categoryPageQueryDTO);
        PageResult page = categoryService.findPage(categoryPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 启用/禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用/禁用分类")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用/禁用分类:{}, {}", status, id);
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 修改套餐/菜品分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result modifyCato(@RequestBody CategoryDTO categoryDTO) {
        log.info("需要修改的数据: {}", categoryDTO);
        categoryService.modifyCato(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result removeCatoById (@RequestParam Long id) {
        log.info("要删除的分类的id为:{}", id);
        categoryService.removeCatoById(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result findByType(Integer type) {
        log.info("根据{}类型查询分类", type);
        List<Category> list = categoryService.findByType(type);
        return Result.success(list);
    }

}
